package com.candy.control;

import com.candy.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       获取前端传递进来的参数
        String username=req.getParameter("username");
        String userpwd=req.getParameter("userpwd");
        System.out.println("username:"+username+";userpwd:"+userpwd);
//        调用业务逻辑处理层，进行比对用户名与密码是否正确
        LoginService service=new LoginService();
        boolean logined=service.login(username,userpwd);
//      进行用户名密码的匹配
        if(logined){
//            将正确的用户信息存储在session中
            req.getSession().setAttribute("user",username);
//            转发
            req.getRequestDispatcher("welcome.jsp").forward(req, resp);
        }else {
//            重定向
            resp.sendRedirect("index.jsp");
        }

    }
}
